Page({
  data: {
    hasResume: false,
    resumeUrl: ''
  },

  onLoad: function() {
    this.checkResume()
  },

  onShow: function() {
    this.checkResume()
  },

  checkResume: async function() {
    try {
      const result = await wx.cloud.callFunction({
        name: 'db',
        data: {
          action: 'getUserResume'
        }
      })
      
      if (result.result && result.result.resumeUrl) {
        this.setData({
          hasResume: true,
          resumeUrl: result.result.resumeUrl
        })
      } else {
        this.setData({
          hasResume: false,
          resumeUrl: ''
        })
      }
    } catch (error) {
      console.error('获取简历信息失败:', error)
      wx.showToast({
        title: '获取简历信息失败',
        icon: 'none'
      })
    }
  },

  // 预览简历
  previewResume: function() {
    if (!this.data.resumeUrl) {
      return
    }

    wx.showLoading({
      title: '加载中...'
    })

    wx.cloud.downloadFile({
      fileID: this.data.resumeUrl,
      success: res => {
        wx.openDocument({
          filePath: res.tempFilePath,
          fileType: 'pdf',
          success: function() {
            console.log('打开文档成功')
          },
          fail: function(err) {
            console.error('打开文档失败：', err)
            wx.showToast({
              title: '打开文档失败',
              icon: 'none'
            })
          }
        })
      },
      fail: err => {
        console.error('下载文件失败：', err)
        wx.showToast({
          title: '下载文件失败',
          icon: 'none'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  uploadResume: function() {
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      extension: ['pdf'],
      success: async (res) => {
        const tempFilePath = res.tempFiles[0].path
        await this.uploadToCloud(tempFilePath)
      },
      fail: (error) => {
        console.error('选择文件失败:', error)
        wx.showToast({
          title: '选择文件失败',
          icon: 'none'
        })
      }
    })
  },

  uploadNewResume: function() {
    this.uploadResume()
  },

  uploadToCloud: async function(tempFilePath) {
    wx.showLoading({
      title: '上传中...'
    })

    try {
      // 上传文件到云存储
      const cloudPath = `resumes/${Date.now()}-${Math.random().toString(36).substr(2)}.pdf`
      const uploadResult = await wx.cloud.uploadFile({
        cloudPath,
        filePath: tempFilePath
      })

      if (uploadResult.fileID) {
        // 更新用户简历信息
        const result = await wx.cloud.callFunction({
          name: 'db',
          data: {
            action: 'updateUserResume',
            resumeUrl: uploadResult.fileID
          }
        })

        if (result.result && result.result.success) {
          wx.showToast({
            title: '上传成功'
          })
          this.checkResume()
          // 上传成功后预览
          this.previewResume()
        } else {
          throw new Error('更新简历信息失败')
        }
      }
    } catch (error) {
      console.error('上传失败:', error)
      wx.showToast({
        title: '上传失败',
        icon: 'none'
      })
    } finally {
      wx.hideLoading()
    }
  }
}) 